package com.filebackup.struts.actions;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.filebackup.business.LocalFolderService;
import com.filebackup.vo.Vo;
import com.filebackup.vo.impl.LocalFolderVo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class FolderSelectAction extends FilebackupSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private LocalFolderService localFolderService;
	
	public String execute() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		JSONArray proArr = getFolder();
		
		response.setContentType("text/html;charset=UTF-8");
		System.out.println("3333");
		PrintWriter out = response.getWriter();
		System.out.println("2222");
		out.print(proArr);
		out.flush();
		out.close();
		return null;
	}
	
	public JSONArray getFolder(){
		JSONArray objArr=new JSONArray();
		List<Vo> voList=this.getLocalFolderService().findRootFolder(this.getSessionUser().getId());
		for(int i=0;i<voList.size();i++){
			JSONObject obj=new JSONObject();
			obj.put("id", ((LocalFolderVo)voList.get(i)).getId());
			obj.put("name", ((LocalFolderVo)voList.get(i)).getFolderName());
			objArr.add(obj);
		}
		return objArr;
	}

	public void setLocalFolderService(LocalFolderService localFolderService) {
		this.localFolderService = localFolderService;
	}

	public LocalFolderService getLocalFolderService() {
		return localFolderService;
	}

}
